﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;

namespace DormMS.Common
{
    public class SmsHelper
    {
        public static bool SendSMSMessage(SMSAccount request)
        {
            //try
            //{
            //    string url = "http://dx.ipyy.net/smsJson.aspx";
            //    //string url = "http://sh2.ipyy.com/smsJson.aspx";
            //    SMSAccount account = new SMSAccount();
            //    Dictionary<string, string> dic = new Dictionary<string, string>();
            //    dic["action"] = account.Action;
            //    dic["userid"] = account.Userid;
            //    dic["account"] = account.Account;
            //    dic["password"] = PassTo.MD5(account.Password);
            //    dic["content"] = content + account.Signature;
            //    dic["sendTime"] = account.SendTime;
            //    dic["extno"] = account.Extno;
            //    dic["mobile"] = mobiles;
            //    string returndata = HttpService.DoPost(url, dic);
            //    LogDB.DebugTest("返回短信消息：" + returndata);
            //    SMSMessage info = new SMSMessage();
            //    if (returndata != "")
            //        info = JsonConvert.DeserializeObject<SMSMessage>(returndata);
            //    if (string.IsNullOrEmpty(info.returnstatus) || info.returnstatus.ToUpper() != "SUCCESS")
            //        return false;
            //    else
            //        return true;
            //}
            //catch (Exception er)
            //{
            //    LogDB.DebugError(er);
            //    return false;
            //}


            try
            {
                if (request.MobileList == null || request.MobileList.Count == 0)
                    return false;
                string url = "https://dx.ipyy.net/ensms.ashx ";
                //string url = "http://sh2.ipyy.com/smsJson.aspx"; 
                string Stamp = Convert.ToDateTime(DateTime.Now).ToString("MMddHHmmss");

                var Secret = SecurityHelper.Md5Encrypt(request.Password + Stamp);

                string mobiles = "";
                request.MobileList.ForEach(m => mobiles += m + ",");

                string json = "{\"UserName\":\"" + request.Account + "\",\"Secret\":\"" + Secret + "\",\"Stamp\":\"" + Stamp + "\",\"Moblie\":\"" + mobiles.TrimEnd(',') + "\",\"Text\":\"" + request.Content + request.Signature + "\",\"Ext\":\"" + "" + "\",\"SendTime\":\"" + request.SendTime + "\"}";

                byte[] data = Encoding.UTF8.GetBytes(json);
                //获取key
                byte[] key = new byte[8];
                byte[] pwd = Encoding.UTF8.GetBytes(request.Password);
                System.Array.Copy(pwd, 0, key, 0, pwd.Length > 8 ? 8 : pwd.Length);
                byte[] b = SecurityHelper.DESEncrypt(data, key);
                //des加密  base64转码
                string str = Convert.ToBase64String(b);

                string Text64 = str;

                string param = "UserId=" + request.Userid + "&Text64=" + HttpUtility.UrlEncode(Text64, Encoding.UTF8);
                byte[] sendData = Encoding.UTF8.GetBytes(param);


                string returndata = RequestHelper.SendPost(url, param);
                SMSMessage info = new SMSMessage();
                if (returndata != "")
                {
                    info = JsonConvert.DeserializeObject<SMSMessage>(returndata);
                    if (info.successCounts == "1")
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }

            }
            catch (Exception er)
            {

                return false;
            }
        }

    }

    public class SMSAccount
    {

        public SMSAccount()
        {
            this.Action = "send";
            this.Account = "AA00620";
            this.Password = "AA0062011";
            this.Signature = "【微视点商城】";
            this.MobileList = new List<string>();

        }
        public string Action { get; set; }
        public string Userid { get; set; }
        public string Account { get; set; }
        public string Password { get; set; }
        public string SendTime { get; set; }
        public string Extno { get; set; }
        public string Signature { get; set; }
        public List<string> MobileList { get; set; }
        public string Content { get; set; }

    }

    public class SMSMessage
    {
        public string returnstatus { get; set; }
        public string message { get; set; }
        public string remainpoint { get; set; }
        public string taskID { get; set; }
        public string successCounts { get; set; }
    }


}
